package com.youku.plugins.interaction.model
{
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import flash.utils.Timer;
   import flash.net.URLLoader;
   import com.youku.plugins.interaction.model.data.InteractionSwfList;
   import com.youku.interfaces.IRootData;
   import com.youku.PlayerConfig;
   import com.youku.interfaces.IPlayListData;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import com.youku.plugins.interaction.model.data.InteractionSwfData;
   import com.adobe.json.JSON;
   import com.youku.plugins.interaction.events.InteractionEvent;
   import flash.events.TimerEvent;
   
   public class InteractionDataProxy extends Object
   {
      
      public function InteractionDataProxy(param1:PluginEventDispather) {
         super();
         this._eventDispather = param1;
         this._rootData = PlayerConfig.rootData;
         this._loadTimer = new Timer(10 * 1000);
         this._loadTimer.addEventListener(TimerEvent.TIMER,this.onLoadTimeOut);
      }
      
      private static const INTERACTIONURL:String = "http://hudong.pl.youku.com/player/getInteractSwf?";
      
      private var _playerProxy:IPlayerProxy;
      
      private var _eventDispather:PluginEventDispather;
      
      private var _loadTimer:Timer;
      
      private var _urlLoader:URLLoader;
      
      private var _interactionSwfList:InteractionSwfList;
      
      private var _rootData:IRootData;
      
      public function loadData(param1:IPlayerProxy) : void {
         this._playerProxy = param1;
         this._interactionSwfList = new InteractionSwfList();
         var _loc2_:IPlayListData = PlayerConfig.playListData;
         var _loc3_:String = INTERACTIONURL;
         _loc3_ = _loc3_ + ("vid=" + this._playerProxy.playerData.videoIdEncode);
         if(this._rootData.pf)
         {
            _loc3_ = _loc3_ + ("&Pf=" + this._rootData.pf);
         }
         if(PlayerConfig.isTudouPlayer)
         {
            _loc3_ = _loc3_ + ("&Sc=" + 2);
         }
         else
         {
            _loc3_ = _loc3_ + ("&Sc=" + 1);
         }
         this._urlLoader = new URLLoader();
         this._urlLoader.addEventListener(Event.COMPLETE,this.onLoadComplete);
         this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.onError);
         this._urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onError);
         this._urlLoader.load(new URLRequest(_loc3_));
         this._loadTimer.reset();
         this._loadTimer.start();
      }
      
      private function onLoadComplete(param1:Event) : void {
         this._loadTimer.stop();
         var _loc2_:String = URLLoader(param1.currentTarget).data;
         this.parseInteractionData(_loc2_);
      }
      
      private function parseInteractionData(param1:String) : void {
         var obj:Object = null;
         var swf:Object = null;
         var swfData:InteractionSwfData = null;
         var datastr:String = param1;
         if(datastr == null || datastr == "")
         {
            this.meetError();
            return;
         }
         try
         {
            obj = com.adobe.json.JSON.decode(datastr);
         }
         catch(error:Error)
         {
            obj = new Object();
         }
         var swfList:Array = [];
         var swfObjs:Array = obj.data as Array;
         if(swfObjs == null || swfObjs.length <= 0)
         {
            this.meetError();
            return;
         }
         for each(swf in swfObjs)
         {
            swfData = new InteractionSwfData();
            if(swf["url"])
            {
               swfData.url = swf["url"];
            }
            if(swf["name"])
            {
               swfData.name = swf["name"];
            }
            if(swf["test_domain"])
            {
               swfData.test_domain = swf["test_domain"];
            }
            swfList.push(swfData);
         }
         this._interactionSwfList.interactionSwfs = swfList;
         this._interactionSwfList.isDataReady = true;
         this._eventDispather.dispatchEvent(new InteractionEvent(InteractionEvent.INTERACTION_DATA_COMPLETE,this._interactionSwfList));
      }
      
      private function onError(param1:Event) : void {
         this._loadTimer.stop();
         this.meetError();
      }
      
      private function onLoadTimeOut(param1:TimerEvent) : void {
         try
         {
            this._urlLoader.close();
         }
         catch(e:Error)
         {
         }
         this._loadTimer.stop();
         this.meetError();
      }
      
      private function meetError() : void {
         this._eventDispather.dispatchEvent(new InteractionEvent(InteractionEvent.INTERACTION_DATA_ERROR));
      }
   }
}
